const app = getApp();
const { request } = require('../api.js'); 

Page({
  data: {
    banners: [
      {
        id: 1,
        imageUrl: 'https://oss.gaoaipangshou.com/uploads/20250724/1382899e1cd208407088214a8474b3d0.jpeg',
        link: '/pages/banner-detail/banner-detail?id=1'
      },
      {
        id: 2,
        imageUrl: 'https://oss.gaoaipangshou.com/uploads/20241001/85ab21d5fa58c7ea013810f1658219c4.jpg',
        link: '/pages/banner-detail/banner-detail?id=2'
      }
    ],
    recommendList: [],
    page: 1,
    hasMore: true,
    loading: false,
    isProfileComplete: false,
    cdnUrl:''
  },

  onLoad() {
    this.setData({
        cdnUrl:app.globalData.cdnUrl
      });
    this.checkLoginStatus();
    this.loadRecommendList();
  },

  onShow() {
    // 每次显示页面时检查资料完整性
    this.setData({
      isProfileComplete: app.checkProfileComplete()
    });
  },

  // 检查登录状态
  checkLoginStatus() {
    if (!app.globalData.isLogin) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            });
          }
        }
      });
    }
  },

  // 加载推荐列表
  loadRecommendList(isRefresh) {
    if (this.data.loading) return;
    this.setData({ loading: true });

    var _this = this
    wx.request({
        url: app.globalData.baseUrl + 'public/recomds',
        data: {
          page: _this.data.page,
        },
        success: function(res) {
          if (res.data.code === 0) {
            const list = res.data.data.needs || []
            const newList = isRefresh ? list : _this.data.recommendList.concat(list)
            
            _this.setData({
              recommendList: newList,
              hasMore: list.length === 5,
              isLoading: false
            })
          }
        },
        complete: function() {
            _this.setData({
            loading: false,
          })
        }
    })
    return
    // 模拟API调用
    setTimeout(() => {
      const mockData = this.getMockRecommendData();
      
      this.setData({
        recommendList: this.data.page === 1 ? mockData : [...this.data.recommendList, ...mockData],
        loading: false,
        hasMore: this.data.page < 3 // 模拟只有3页数据
      });
    }, 1000);
  },

  // 获取模拟数据
  getMockRecommendData() {
    const mockUsers = [
      {
        id: 1,
        nickname: '温柔如风',
        age: 58,
        income: '月收入8000+',
        city: '北京市朝阳区',
        description: '退休教师，喜欢读书、旅游，希望找一个志同道合的伴侣共度晚年。',
        avatar: '/images/avatar/11.jpg',
        datingType: 'partner'
      },
      {
        id: 2,
        nickname: '阳光大叔',
        age: 62,
        income: '月收入12000+',
        city: '北京市海淀区',
        description: '工程师退休，性格开朗，喜欢运动，寻找真诚的另一半。',
        avatar: '/images/avatar/22.jpg',
        datingType: 'partner'
      },
      {
        id: 3,
        nickname: '优雅女士',
        age: 55,
        income: '月收入6000+',
        city: '北京市西城区',
        description: '会计工作，生活规律，喜欢烹饪和园艺，希望遇到有缘人。',
        avatar: '/images/avatar/33.jpg',
        datingType: 'friend'
      }
    ];
    
    return mockUsers;
  },

  // Banner点击
  onBannerTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/banner-detail/banner-detail?id=${id}`
    });
  },

  // 查看用户资料
  viewProfile(e) {
    const id = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: `/pages/user-detail/user-detail?id=${id}`
    });
  },

  // 打招呼
  sayHello(e) {
    if (!app.globalData.isLogin) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            });
          }
        }
      });
      return;
    }

    /*if (!app.globalData.isRealNameAuth) {
      wx.showModal({
        title: '提示',
        content: '请先完成实名认证',
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/real-name-auth/real-name-auth'
            });
          }
        }
      });
      return;
    }*/

    let _this = this
    const id = e.currentTarget.dataset.userid;
    const index = this.data.recommendList.findIndex(item => item.user_id === id);

    request({
        url: app.globalData.baseUrl + 'api/chat-call?obj_user_id='+id,
    }).then(res => {
         // 更新该用户的打招呼状态
        let recommendList = _this.data.recommendList;
        recommendList[index].is_called = true; // 标记为已打招呼
        _this.setData({
            recommendList: recommendList,
        });
        wx.showToast({
            title: "已打招呼，等TA回应",
            icon: 'none'
        })
    })
  },

  // 加载更多
  loadMore() {
    if (!this.data.hasMore || this.data.loading) return;
    
    this.setData({
      page: this.data.page + 1
    });
    
    this.loadRecommendList();
  },

  // 去完善资料
  goToEditProfile() {
    wx.navigateTo({
      url: '/pages/edit-profile/edit-profile'
    });
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.setData({
      page: 1,
      hasMore: true
    });
    this.loadRecommendList();
    wx.stopPullDownRefresh();
  },

  // 上拉加载
  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      this.loadMore();
    }
  }
}); 